package com.community.body.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.community.body.domain.ZyRepair;
import com.community.body.dto.ServicemanDTO;
import com.community.body.mapper.ZyFilesMapper;
import com.community.body.mapper.ZyRepairMapper;
import com.community.body.service.IZyRepairService;
import com.community.body.vo.EchartsVo;
import com.community.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 报修信息Service业务层处理
 *
 * @author community
 * @date 2024-04-19
 */
@Service
public class ZyRepairServiceImpl implements IZyRepairService {
    @Autowired
    private ZyRepairMapper zyRepairMapper;

    @Resource
    private RedisTemplate<String, Long> redisTemplate;

    @Autowired
    private ZyFilesMapper zyFilesMapper;

    /**
     * 查询报修信息
     *
     * @param repairId 报修信息主键
     * @return 报修信息
     */
    @Override
    public ZyRepair selectZyRepairByRepairId(Long repairId) {
        return zyRepairMapper.selectZyRepairByRepairId(repairId);
    }

    /**
     * 查询报修信息列表
     *
     * @param servicemanDTO 报修信息
     * @return 报修信息
     */
    @Override
    public List<ServicemanDTO> selectZyRepairList(ServicemanDTO servicemanDTO) {
        if (servicemanDTO.getUserName() != null && !servicemanDTO.getUserName().isEmpty() && servicemanDTO.getUserName().equals("admin")) {
            servicemanDTO.setDeptId(0L);
        }
        return zyRepairMapper.selectZyRepairList(servicemanDTO);
    }

    /**
     * 分配维修人员
     *
     * @param servicemanDTO
     * @return
     */
    @Override
    public List<ServicemanDTO> selectZyRepairServiceman(ServicemanDTO servicemanDTO) {
        if (servicemanDTO.getUserName() != null && !servicemanDTO.getUserName().isEmpty() && servicemanDTO.getUserName().equals("admin")) {
            servicemanDTO.setDeptId(0L);
        }
        return zyRepairMapper.selectZyRepairServiceman(servicemanDTO);
    }

    /**
     * 新增报修信息
     *
     * @param zyRepair 报修信息
     * @return 结果
     */
    @Override
    public int insertZyRepair(ZyRepair zyRepair) {
        ZyRepair repair = new ZyRepair();
        repair.setAddress(zyRepair.getAddress());
        repair.setRepairContent(zyRepair.getRepairContent());
        repair.setUserId(zyRepair.getUserId());
        repair.setOwnerName(zyRepair.getOwnerName());
        repair.setOwnerPhone(zyRepair.getOwnerPhone());
        repair.setDoorTime(zyRepair.getDoorTime());
        repair.setCommunityId(zyRepair.getCommunityId());
        repair.setRepairNum("BX_" + System.currentTimeMillis());
        repair.setRepairState("Repairing");
        repair.setCreateTime(DateUtils.getNowDate());
        int i = zyRepairMapper.insertZyRepair(repair);

        redisTemplate.opsForValue().set("repairId", repair.getRepairId());
        return i;
    }

    /**
     * 修改报修信息
     *
     * @param zyRepair 报修信息
     * @return 结果
     */
    @Override
    public int updateZyRepair(ZyRepair zyRepair) {
        // 已分配
        if (zyRepair.getRepairState().equals("Allot")) {
            zyRepair.setAssignmentTime(new Date());
            zyRepair.setReceivingOrdersTime(new Date());
        }
        // 已维修
        if (zyRepair.getRepairState().equals("Repaired")) {
            zyRepair.setCompleteTime(new Date());
        }
        zyRepair.setUpdateTime(DateUtils.getNowDate());
        return zyRepairMapper.updateZyRepair(zyRepair);
    }

    /**
     * 批量删除报修信息
     *
     * @param repairIds 需要删除的报修信息主键
     * @return 结果
     */
    @Override
    public int deleteZyRepairByRepairIds(Long[] repairIds) {
        ZyRepair zyRepair = zyRepairMapper.selectZyRepairByRepairId(repairIds[0]);
        zyRepair.setDelFlag(1L);
        return zyRepairMapper.updateZyRepair(zyRepair);
    }

    /**
     * 删除报修信息信息
     *
     * @param repairId 报修信息主键
     * @return 结果
     */
    @Override
    public int deleteZyRepairByRepairId(Long repairId) {
        ZyRepair zyRepair = zyRepairMapper.selectZyRepairByRepairId(repairId);
        zyRepair.setDelFlag(1L);
        return zyRepairMapper.updateZyRepair(zyRepair);
    }

    @Override
    public List<EchartsVo> getRepairRate(ServicemanDTO servicemanDTO) {
        if (servicemanDTO.getCreateBy() != null){
            String[] split = servicemanDTO.getCreateBy().split(",");
            Long[] longArray = Arrays.stream(split)
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toArray(Long[]::new);
            return zyRepairMapper.getRepairRate1(longArray);
        }
        return zyRepairMapper.getRepairRate(servicemanDTO);
    }
}
